package com.gjy.nwpufindseats.Fragment;

import java.util.ArrayList;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.gjy.nwpufindseats.baseData.AllGrade;
import com.gjy.nwpufindseats.baseData.Session;
import com.gjy.nwpufindseats.R;

public  class Fragment_grade_info extends Fragment {
	private TableLayout gradeTableLayout;
	private LinearLayout gradeLinearLayout;
	public static ArrayList<String> gradeArrayList=new ArrayList<String>();
	private View grade_info;
	private TextView gradeTitle;
	Boolean xueba = false;
     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
		Bundle bundle = getArguments();
    	grade_info = inflater.inflate(R.layout.fragment_grade_info, container, false);	
    	gradeLinearLayout = (LinearLayout)grade_info.findViewById(R.id.gradeLinearLayout);
    	gradeTableLayout= (TableLayout) grade_info.findViewById(R.id.gradesTable);//成绩表格
    	gradeArrayList = bundle.getStringArrayList("grade_info");
    	gradeTitle = (TextView)grade_info.findViewById(R.id.gradeTitle);
    	if(0==bundle.getInt("gradeType"))
			{
    			gradeTitle.setText("本学期成绩");
			}
			else if(1==bundle.getInt("gradeType")){
				gradeTitle.setText("课程属性查询");
			}
			else if(2==bundle.getInt("gradeType"))
			{
				gradeTitle.setText("不及格成绩");
				if(4==gradeArrayList.size())	
				{
					 LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
		                     LinearLayout.LayoutParams.WRAP_CONTENT);
		    		mParams.gravity = Gravity.CENTER_HORIZONTAL;
		    		TextView good = new TextView(getActivity());
		    		good.setText("学霸你好！\n您这么点进来是要干什么？\n\n难道\n是在帮我们找BUG吗？\n\n您还是继续学霸吧\n别再欺负我这脆弱的功能了");
		    		good.setTextColor(Color.parseColor("#fa9199"));
		    		good.setLayoutParams(mParams);
		    		good.setGravity(Gravity.CENTER);
		    		good.setTextSize(20);
		    		gradeLinearLayout.addView(good);   
		    		xueba = true;
		    		return grade_info;
				}
			}
			else {//由于传的类型不同，故此页面在此进行显示
				gradeTitle.setText("全部及格成绩");
				Session session = Session.getSession();
				@SuppressWarnings("unchecked")
				ArrayList<AllGrade> allGrades = (ArrayList<AllGrade>) session.get("allGrades");
				int length = allGrades.size();
				for(int i=length-1;i>-1;i--)
				{
					TextView term = new TextView(getActivity());
					term.setText(allGrades.get(i).getTerm());
					term.setTextColor(Color.parseColor("#fa9199"));
					term.setTextSize(24);
					term.setGravity(Gravity.LEFT);
					gradeTableLayout.addView(term); 
					int grade_length = allGrades.get(i).getGradeList().size();
					for(int j=0;j<grade_length;)
			    	{    		
			    		TableRow tbRow =new TableRow(getActivity());
			    		TextView gradeName = new TextView (getActivity());
			    		TextView garde = new TextView (getActivity());
			    		gradeName.setText(allGrades.get(i).getGradeList().get(j));
			    		gradeName.setTextSize(20);
			    		gradeName.setGravity(Gravity.LEFT);
			    		gradeName.setTextColor(Color.parseColor("#000000"));
			    		if(j%4==0)
			    		{
			    			gradeName.setBackgroundColor(Color.parseColor("#c7f7d7"));
			    			garde.setBackgroundColor(Color.parseColor("#c7f7d7"));
			    		}
			    		tbRow.addView(gradeName);    		
			    		j++;
			    		garde.setText(allGrades.get(i).getGradeList().get(j));
			    		garde.setTextSize(20);
			    		garde.setGravity(Gravity.LEFT);
			    		garde.setTextColor(Color.parseColor("#000000"));
			    		tbRow.addView(garde);
			    		gradeTableLayout.addView(tbRow);
			    		j++;
			    	}
				}
				session.remove("allGrades");
			return  grade_info;
			}
			
				
    	if(gradeArrayList.size() == 1)
    		{
    		 LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                     LinearLayout.LayoutParams.WRAP_CONTENT);
    		mParams.gravity = Gravity.CENTER_HORIZONTAL;
    		TextView hasGrade = new TextView(getActivity());
    		hasGrade.setText("本学期还没有考试哦！");
    		hasGrade.setTextColor(Color.parseColor("#fa9199"));
    		hasGrade.setLayoutParams(mParams);
    		hasGrade.setGravity(Gravity.CENTER);
    		gradeLinearLayout.addView(hasGrade);   		
    		return grade_info;
    		}
    	for(int i=0;i<gradeArrayList.size();)
    	{    		
    		TableRow tbRow =new TableRow(getActivity());
    		TextView gradeName = new TextView (getActivity());
    		TextView garde = new TextView (getActivity());
    		gradeName.setText(gradeArrayList.get(i));
    		if("尚不及格".equals(gradeArrayList.get(i)) || "曾不及格".equals(gradeArrayList.get(i)))
    		{
    			gradeName.setTextSize(20);
    			gradeName.setTextColor(Color.parseColor("#ff0000"));
    		}
    		else {
    			gradeName.setTextSize(20);
    			gradeName.setTextColor(Color.parseColor("#000000"));
			} 
		
    		gradeName.setGravity(Gravity.LEFT);
    		
    		if(i%4==0)
    		{
    			gradeName.setBackgroundColor(Color.parseColor("#c7f7d7"));
    			garde.setBackgroundColor(Color.parseColor("#c7f7d7"));
    		}
    		tbRow.addView(gradeName);    		
    		i++;
    		garde.setText(gradeArrayList.get(i));
    		garde.setTextSize(20);
    		garde.setGravity(Gravity.LEFT);
    		garde.setTextColor(Color.parseColor("#000000"));
    		tbRow.addView(garde);
    		gradeTableLayout.addView(tbRow);
    		i++;
    	}
    	return grade_info;
    }	
}

